// 同时发送异步代码的次数
let ajaxTimes = 0;
var app = getApp()
export const request = (params, showLoading = true) => {
  if (app.globalData.userInfo == null && app.globalData.loginFlag == false) {
    wx.showModal({
      title: '提示',
      content: '您还未登录，是否跳转登录？',
      success: res=>{
        if(res.confirm){
          wx.switchTab({
            url: '/pages/myPage/myPage',
          })
        }
      }
    })
  } else {
    ajaxTimes++;
    // 显示加载中效果
    if (showLoading) {
      wx.showLoading({
        title: '加载中',
        mask: true
      });
    }
    //定义公共的url
    // 172.16.7.167:3000
    const baseUrl = "http://yingxiao.ztmbec1.com:7777/api/private/v1";
    return new Promise((resolve, reject) => {
      wx.request({
        ...params,
        url: baseUrl + params.url,
        success: (result) => {
          resolve(result);
        },
        fail: (err) => {
          reject(err)
        },
        complete: () => {
          ajaxTimes--;
          if (showLoading && (ajaxTimes === 0)) {
            // 关闭图标
            wx.hideLoading();
          }
        }
      });
    })
  }
}